package com.ming.springbillmanager.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @project_name: spring2
 * @description:
 * @author: arvin w ming
 * @create: 2022-09-08 14:08
 */

//自定义拦截器
public class LoginHanderInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //step 1: 获取用户名
        Object loginUser = request.getSession().getAttribute("loginUser");

        //step 2: 判断用户名是否存在
        if(loginUser != null){
            //说明已经登录过，方行
            return true;
        }
        //step 3：用户名为空，说明从来没有登录过
        request.setAttribute("message","不好意思，没有权限，请先登录");
        request.getRequestDispatcher("/index.html").forward(request,response);
        return false;
    }
}
